home *** CD-ROM | disk | FTP | other *** search
/ Aminet 52 / Aminet 52 (2002)(GTI - Schatztruhe)[!][Dec 2002].iso / Aminet / dev / gg / ncurses-5.3.lha / ncurses-5.3 / Ada95 / samples / sample-keyboard_handler.adb < prev    next >
Text File  |  2002-10-24  |  8KB  |  193 lines

  1. ------------------------------------------------------------------------------
  2. --                                                                          --
  3. --                       GNAT ncurses Binding Samples                       --
  4. --                                                                          --
  5. --                            Sample.Keyboard_Handler                       --
  6. --                                                                          --
  7. --                                 B O D Y                                  --
  8. --                                                                          --
  9. ------------------------------------------------------------------------------
  10. -- Copyright (c) 1998 Free Software Foundation, Inc.                        --
  11. --                                                                          --
  12. -- Permission is hereby granted, free of charge, to any person obtaining a  --
  13. -- copy of this software and associated documentation files (the            --
  14. -- "Software"), to deal in the Software without restriction, including      --
  15. -- without limitation the rights to use, copy, modify, merge, publish,      --
  16. -- distribute, distribute with modifications, sublicense, and/or sell       --
  17. -- copies of the Software, and to permit persons to whom the Software is    --
  18. -- furnished to do so, subject to the following conditions:                 --
  19. --                                                                          --
  20. -- The above copyright notice and this permission notice shall be included  --
  21. -- in all copies or substantial portions of the Software.                   --
  22. --                                                                          --
  23. -- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS  --
  24. -- OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF               --
  25. -- MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.   --
  26. -- IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,   --
  27. -- DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR    --
  28. -- OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR    --
  29. -- THE USE OR OTHER DEALINGS IN THE SOFTWARE.                               --
  30. --                                                                          --
  31. -- Except as contained in this notice, the name(s) of the above copyright   --
  32. -- holders shall not be used in advertising or otherwise to promote the     --
  33. -- sale, use or other dealings in this Software without prior written       --
  34. -- authorization.                                                           --
  35. ------------------------------------------------------------------------------
  36. --  Author:  Juergen Pfeifer, 1996
  37. --  Contact: http://www.familiepfeifer.de/Contact.aspx?Lang=en
  38. --  Version Control
  39. --  $Revision: 1.8 $
  40. --  Binding Version 01.00
  41. ------------------------------------------------------------------------------
  42. with Ada.Strings; use Ada.Strings;
  43. with Ada.Strings.Fixed; use Ada.Strings.Fixed;
  44. with Ada.Strings.Maps.Constants; use Ada.Strings.Maps.Constants;
  45. with Ada.Characters.Latin_1; use Ada.Characters.Latin_1;
  46. with Ada.Characters.Handling; use Ada.Characters.Handling;
  47.  
  48. with Terminal_Interface.Curses.Panels; use Terminal_Interface.Curses.Panels;
  49. with Terminal_Interface.Curses.Forms; use Terminal_Interface.Curses.Forms;
  50. with Terminal_Interface.Curses.Forms.Field_Types.Enumeration;
  51. use  Terminal_Interface.Curses.Forms.Field_Types.Enumeration;
  52.  
  53. with Sample.Header_Handler; use Sample.Header_Handler;
  54. with Sample.Form_Demo.Aux; use Sample.Form_Demo.Aux;
  55. with Sample.Manifest; use Sample.Manifest;
  56. with Sample.Form_Demo.Handler;
  57.  
  58. --  This package contains a centralized keyboard handler used throughout
  59. --  this example. The handler establishes a timeout mechanism that provides
  60. --  periodical updates of the common header lines used in this example.
  61. --
  62.  
  63. package body Sample.Keyboard_Handler is
  64.  
  65.    In_Command : Boolean := False;
  66.  
  67.    function Get_Key (Win : Window := Standard_Window) return Real_Key_Code
  68.    is
  69.       K : Real_Key_Code;
  70.  
  71.       function Command return Real_Key_Code;
  72.  
  73.  
  74.       function Command return Real_Key_Code
  75.       is
  76.          function My_Driver (F : Form;
  77.                              C : Key_Code;
  78.                              P : Panel) return Boolean;
  79.          package Fh is new Sample.Form_Demo.Handler (My_Driver);
  80.  
  81.          type Label_Array is array (Label_Number) of String (1 .. 8);
  82.  
  83.          Labels : Label_Array;
  84.  
  85.          FA : Field_Array_Access := new Field_Array'
  86.            (Make (0, 0, "Command:"),
  87.             Make (Top => 0, Left => 9, Width => Columns - 11),
  88.             Null_Field);
  89.  
  90.          K  : Real_Key_Code := Key_None;
  91.          N  : Natural := 0;
  92.  
  93.          function My_Driver (F : Form;
  94.                              C : Key_Code;
  95.                              P : Panel) return Boolean
  96.          is
  97.             Ch : Character;
  98.          begin
  99.             if C in User_Key_Code'Range and then C = QUIT then
  100.                if Driver (F, F_Validate_Field) = Form_Ok  then
  101.                   K := Key_None;
  102.                   return True;
  103.                end if;
  104.             elsif C in Normal_Key_Code'Range then
  105.                Ch := Character'Val (C);
  106.                if (Ch = LF or else Ch = CR) then
  107.                   if Driver (F, F_Validate_Field) = Form_Ok  then
  108.                      declare
  109.                         Buffer : String (1 .. Positive (Columns - 11));
  110.                         Cmdc : String (1 .. 8);
  111.                      begin
  112.                         Get_Buffer (Fld => FA (2), Str => Buffer);
  113.                         Trim (Buffer, Left);
  114.                         if Buffer (1) /= ' ' then
  115.                            Cmdc := To_Upper (Buffer (Cmdc'Range));
  116.                            for I in Labels'Range loop
  117.                               if Cmdc = Labels (I) then
  118.                                  K := Function_Key_Code
  119.                                    (Function_Key_Number (I));
  120.                                  exit;
  121.                               end if;
  122.                            end loop;
  123.                         end if;
  124.                         return True;
  125.                      end;
  126.                   end if;
  127.                end if;
  128.             end if;
  129.             return False;
  130.          end My_Driver;
  131.  
  132.       begin
  133.          In_Command := True;
  134.          for I in Label_Number'Range loop
  135.             Get_Soft_Label_Key (I, Labels (I));
  136.             Trim (Labels (I), Left);
  137.             Translate (Labels (I), Upper_Case_Map);
  138.             if Labels (I) (1) /= ' ' then
  139.                N := N + 1;
  140.             end if;
  141.          end loop;
  142.          if N > 0 then --  some labels were really set
  143.             declare
  144.                Enum_Info    : Enumeration_Info (N);
  145.                Enum_Field   : Enumeration_Field;
  146.                J : Positive := Enum_Info.Names'First;
  147.  
  148.                Frm : Form := Create (FA);
  149.  
  150.             begin
  151.                for I in Label_Number'Range loop
  152.                   if Labels (I) (1) /= ' ' then
  153.                      Enum_Info.Names (J) := new String'(Labels (I));
  154.                      J := J + 1;
  155.                   end if;
  156.                end loop;
  157.                Enum_Field := Create (Enum_Info, True);
  158.                Set_Field_Type (FA (2), Enum_Field);
  159.                Set_Background (FA (2), Normal_Video);
  160.  
  161.                Fh.Drive_Me (Frm, Lines - 3, 0);
  162.                Delete (Frm);
  163.                Update_Panels; Update_Screen;
  164.             end;
  165.          end if;
  166.          Free (FA, True);
  167.          In_Command := False;
  168.          return K;
  169.       end Command;
  170.  
  171.    begin
  172.       Set_Timeout_Mode (Win, Delayed, 30000);
  173.       loop
  174.          K := Get_Keystroke (Win);
  175.          if K = Key_None then  -- a timeout occured
  176.             Update_Header_Window;
  177.          elsif K = 3 and then not In_Command  then  -- CTRL-C
  178.             K := Command;
  179.             exit when K /= Key_None;
  180.          else
  181.             exit;
  182.          end if;
  183.       end loop;
  184.       return K;
  185.    end Get_Key;
  186.  
  187.    procedure Init_Keyboard_Handler is
  188.    begin
  189.       null;
  190.    end Init_Keyboard_Handler;
  191.  
  192. end Sample.Keyboard_Handler;
  193.